package com.webdemo04.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdemo04.bean.User;
import com.webdemo04.util.DBHandler;
@Namespace(value="/jsp")
@Action(value="QueryUser",results={@Result(location="/WEB-INF/jsp/show.jsp")})
public class QueryUser extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		Connection conn = DBHandler.newInstance().getConnection();
		String sql = "select id,name from annotation01_user";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		List<User> users = new ArrayList<User>();
		while(rs.next()){
			User user = new User();
			user.setId(rs.getInt("id"));
			user.setName(rs.getString("name"));
			users.add(user);
		}
		ActionContext.getContext().put("users", users);
		return SUCCESS;
	}

}
